package cn.limeizi.ssm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.limeizi.ssm.po.Config;
import cn.limeizi.ssm.service.ConfigService;

@Controller
@RequestMapping("/admin")
public class AdminConfigController {
	
	@Autowired
	private ConfigService configService;
	
	@RequestMapping(value="/adminConfig.action",method=RequestMethod.GET)
	public String index(Model model){
		
		List<Config> configList = configService.selectAll();
		
		model.addAttribute("configList",configList);
		
		return "admin/config/configList";
			
	}
	
	@RequestMapping(value="/toEditConfig.action",method=RequestMethod.GET)
	public String toEditConfig(Model model,Integer id){
		
		if(id!=null){
			
			Config config=configService.selectById(id);
			
			model.addAttribute("config", config);
		}
		
		return "admin/config/edit";		
	}
	
	@RequestMapping(value="/editConfig.action",method=RequestMethod.POST)
	public String edit(Config config){
		
		if(config.getId()!=null){
			configService.update(config);
		}else{
			configService.insert(config);
		}
		
		config.setStatus(1);
		
		return "redirect:adminConfig.action";	
	}
	
	@RequestMapping(value="/deleteConfig.action",method=RequestMethod.GET)
	public String delete(Integer id){
		
		configService.delete(id);;
		return "redirect:adminConfig.action";
	}

}
